package github.kawaiior.yuanchuxiuzhen.common.item.weapon.refine;

import github.kawaiior.yuanchuxiuzhen.util.EntityUtil;
import github.kawaiior.yuanchuxiuzhen.util.MathUtil;
import net.minecraft.block.Blocks;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

import java.util.List;

public class EarthShakeHammer extends RefineWeaponBase{

    @Override
    public float getAttackDamage(){
        return 34F;
    }

    @Override
    public float getAttackSpeed(){
        return -2.2F;
    }

    @Override
    public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
        if (worldIn.isRemote) {
            return super.onItemRightClick(worldIn, playerIn, handIn);
        }

        if (playerIn.isSneaking()){
            AxisAlignedBB boundingBox = playerIn.getBoundingBox().grow(10.0);
            List<Entity> entities = worldIn.getEntitiesWithinAABB(Entity.class, boundingBox);
            for (Entity entity : entities) {
                if (entity instanceof LivingEntity && !entity.equals(playerIn)) {
                    EntityUtil.playerMotionEntity((LivingEntity)entity, playerIn, 0, 10, 0, 0, 5, 0);
                }
            }
            BlockPos pos = playerIn.getPosition();
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    BlockPos p = pos.add(i-1, 0, j-1);
                    if (worldIn.getBlockState(p).getBlock() == Blocks.AIR){
                        worldIn.destroyBlock(p.add(0, -1, 0), true);
                    }else {
                        worldIn.destroyBlock(p, true);
                    }
                }
            }
            return super.onItemRightClick(worldIn, playerIn, handIn);
        }

        Entity entity = EntityUtil.getPlayerLookTarget(worldIn, playerIn, 5);
        if (entity instanceof LivingEntity){
            EntityUtil.playerMotionEntity((LivingEntity)entity, playerIn, 0, 10, 0, 0, 5, 0);
        }

        return super.onItemRightClick(worldIn, playerIn, handIn);
    }

    @Override
    public boolean hitEntity(ItemStack stack, LivingEntity target, LivingEntity attacker) {
        if (MathUtil.checkProbability(0.1F)){
            EntityUtil.playerMotionEntity(target, attacker, 3, 10, 3, 0, 5, 0);
        }
        return super.hitEntity(stack, target, attacker);
    }

}
